/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Project;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.annotation.Resource;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Named;
import javax.sql.DataSource;

/**
 *
 * @author Luke Nomeland
 */
@Named
@ConversationScoped
public class CarBean implements Serializable {

    @Resource(name = "jdbc/project")
    private DataSource ds;
    // car info
    private String username;
    private String title;
    private String make;
    private String model;
    private String color;
    private int price;
    private int year;
    private int miles;
    private String description;
    //navigation constants
    private String postListing = "listing";
    private String homePage = "home";
    private String searchPage = "search";
    private String manageCar = "manage";

    public CarBean() {
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String newValue) {
        this.username = newValue;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String newValue) {
        this.title = newValue;
    }

    public String getMake() {
        return this.make;
    }

    public void setMake(String newValue) {
        this.make = newValue;
    }

    public String getModel() {
        return this.model;
    }

    public void setModel(String newValue) {
        this.model = newValue;
    }

    public String getColor() {
        return this.color;
    }

    public void setColor(String newValue) {
        this.color = newValue;
    }

    public int getYear() {
        return this.year;
    }

    public void setYear(int newValue) {
        this.year = newValue;
    }

    public int getPrice() {
        return this.price;
    }

    public void setPrice(int newValue) {
        this.price = newValue;
    }

    public int getMiles() {
        return this.miles;
    }

    public void setMiles(int newValue) {
        this.miles = newValue;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String newValue) {
        this.description = newValue;
    }

    // called when the post listing button is clicked
    public String postListing() throws SQLException {
        Connection postConnection = ds.getConnection();
        try {
            Statement statement = postConnection.createStatement();
            statement.executeUpdate("INSERT INTO car (username,make,model,year,miles,color,price,title,description)"
                    + "VALUES(" + this.username + "," + this.make + "," + this.model + "," + this.year + "," + this.miles + "," + this.color
                    + "," + this.price + "," + this.title + "," + this.description + ")");
        } catch (SQLException e) {
            System.out.println("SQL ERROR: POST LISTING");
        } finally {
            postConnection.close();
        }

        return postListing;
    }

    // navigates to the home page
    public String home() {
        return this.homePage;
    }
    
    /*
     * Method used to show attributes of a car
     *
     */
    public String viewCar(int carID)
            throws SQLException {
        String query = "SELECT "
                + "car.model, car.make, car.mileage, car.price, car.'year', car.color, users.username, car.description"
                + "FROM 'car', 'users'"
                + "WHERE car.carID = carID"
                + "AND car.username = users.userID";

        Connection connection = ds.getConnection();
        try {
            Statement statement = connection.createStatement();
            ResultSet result = statement.executeQuery(query);
            this.make = result.getString("model");
            this.model = result.getString("make");
            this.miles = result.getInt("mileage");
            this.price = result.getInt("price");
            this.year = result.getInt("year");
            this.username = result.getString("username");
            this.description = result.getString("description");
            return postListing;
        } finally {
            connection.close();
        }
    }
}
